const relationSchema = {
  val: 0,
  left: {
    val: 1,
    left: {
      val: 3,
      left: { val: 7 },
      right: { val: 8 },
    },
    right: {
      val: 4,
      left: {
        val: 9,
        left: {
          val: 11,
          left: {
            val: 12,
            left: {
              val: 13,
            },
          },
          right: { val: 10 },
        },
      },
      right: { val: 10 },
    },
  },
  right: {
    val: 2,
    left: {
      val: 5,
    },
    right: {
      val: 6,
    },
  },
};
function FindPath(root, expectNumber) {
  const result = []; 
  if (root) {
    FindPathCore(root, expectNumber, [], 0, result);
  }
  return result;
}

function FindPathCore(node, expectNumber, stack, sum, result) {
  stack.push(node.val);
  sum += node.val;
  if (sum === expectNumber) {
    result.push(...stack.slice(0));
  }
  if (node.left) {
    FindPathCore(node.left, expectNumber, stack, sum, result);
  }
  if (node.right) {
    FindPathCore(node.right, expectNumber, stack, sum, result);
  }
  stack.pop();
}

console.log(FindPath(relationSchema, 37));
